package cn.itcast.bos.service.action.teke_delivary.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.action.teke_delivary.UserService;
import cn.itcast.bos.web.Dao.teke_delivary.RoleRepository;
import cn.itcast.bos.web.Dao.teke_delivary.UserRepository;

@Service
@Transactional
public class UserServiceimpl implements UserService {

	// 注入Repository
	@Autowired
	private UserRepository userRepository;

	@Override
	public User findByUsername(String username) {
		return userRepository.findByUsername(username);
	}

	// 查询
	@Override
	public List<User> findAll() {

		return userRepository.findAll();
	}

	// 进行用户的添加

	// 注入Role的注入
	@Autowired
	private RoleRepository roleRepository;

	@Override
	public void userSave(String[] roleIds, User model) {
		//保存用户
		userRepository.save(model);
		// 进行角色关联       授予角色
		if (roleIds != null) {
			for (String roleId : roleIds) {
				Role roleint = roleRepository.findOne(Integer.parseInt(roleId));
				model.getRoles().add(roleint);
			}
		}

	}

}
